#coding=utf-8
import urllib
import re,urllib2
import time
import getpass
import os
def getHtml(url):
    page = urllib.urlopen(url)
	try:
		page = urllib.urlopen(url)
		html = page.read()
        	return html
	except Exception, e:
		return e
web="shuaitq.wicp.net"
if not os.path.isfile(web+".log"):
	print("shuaitq.wicp.net is not find")
	web=raw_input("Please input your web address:\n")
	if not os.path.isfile(web+".log"):
		print(web+" is not find")
		name=raw_input("Please input your Oray name:\n")
		password=getpass.getpass("Please input your pass word:\n")
		fp=open(web+".log","a+")
		fp.write(name+"\n")
		fp.write(password+"\n")
	else:
		fp=open(web+".log","a+")
		name=fp.readline()
        	name=name.strip()
		password=fp.readline()
		name=name.strip()
else:
	fp=open(web+".log","a+")
	name=fp.readline()
	name=name.strip()
	password=fp.readline()
	password=password.strip()
while True:
	try:
		ip=re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http://1111.ip138.com/ic.asp").read()).group(0)
		flag=True
	except Exception, e:
		flag=False
		ip=e
	print (time.asctime(time.localtime(time.time())))
	fp.write(time.asctime(time.localtime(time.time()))+"\n")
	print (ip)
	fp.write(str(ip)+"\n")
	if flag:
		html = getHtml("http://"+name+":"+password+"@ddns.oray.com/ph/update?hostname="+web+"&myip="+ip)
		print html
		fp.write(str(html)+"\n")
	else:
		print "Don't get ip"
		fp.write("Don't get ip\n")
	fp.flush()
	time.sleep(600)
fp.close()